package com.jimikongjian.controller;

import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.models.UserInfo;
import com.jimikongjian.service.base.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * Created by baidu on 17/2/14.
 * @author fuxiuyang
 */
@RestController("userController")
public class UserController {

    @Autowired
    @Qualifier("userService")
    private IUserService userService;

    @RequestMapping(value = "/login" , method = RequestMethod.GET)
    @ResponseBody
    public String sayHello(){
        return "Hello!";
    }


    /**
     * 接收前端发过来的登录请求，并验证，并将验证结果返回给前端
     * @param map
     * @return 将验证信息返给前端
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage login(@RequestBody Map<String,String> map){
        String email = "";
        String password = "";
        String uuid = "";
        String code = "";

        if(map.containsKey("email")){
            email = map.get("email");
            if (email == null){
                email = "";
            }
        }
        if (map.containsKey("password")){
            password = map.get("password");
            if (password == null){
                password = "";
            }
        }
        if (map.containsKey("uuid")){
            uuid = map.get("uuid");
            if (uuid == null){
                uuid = "";
            }
        }
        if (map.containsKey("code")){
            code = map.get("code");
            if (code == null){
                code = "";
            }
        }

//        这里应该加一个验证码的验证

        return userService.login(email, password, uuid, code);

    }

    @RequestMapping(value = "/login_code", method = RequestMethod.GET)
    public ResponseMessage defaultKaptcha() throws Exception{
        return userService.getCodeImage();
    }

    /**
     * 用户登出
     * @param map
     */
    public void logout(@RequestBody Map<String, String> map){
        String email = "";
        if (map.containsKey("email")){
            email = map.get("email");
            System.out.println("logout id is :" + email);
        }
        userService.logout(email);
    }

    /**
     * 用户注册接口
     * @param map
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage register(@RequestBody Map<String, String> map){
        String email = "";
        String password  = "";
        String nick = "";
        String code = "";
        if (map.containsKey("email")){
            email = map.get("email");
            if (email == null){
                email = "";
            }
            System.out.println(email);
        }
        if (map.containsKey("password")){
            password = map.get("password");
            if (password == null){
                password = "";
            }
            System.out.println(password);
        }
        if (map.containsKey("nick")){
            nick = map.get("nick");
            if (nick == null){
                nick = "";
            }
            System.out.println(nick);
        }
        if (map.containsKey("code")){
            code = map.get("code");
            if (code == null){
                code = "";
            }
            System.out.println(code);
        }
//        这里也应该加一个验证码，实现短信邮箱发送验证码
        return userService.register(email, password, nick, code);
    }

    /**
     * 重置密码第一步
     * @param map
     * @return
     */
    @RequestMapping(value = "/reset_password01",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage resetPassword01(@RequestBody Map<String, String> map){
        String email = "";
        String code = "";
        if (map.containsKey("email")){
            email = map.get("email");
            if (email == null){
                email = "";
            }
        }
        if (map.containsKey("code")){
            code = map.get("code");
            if (code == null){
                code = "";
            }
        }
        return userService.updatePassWord01(email, code);
    }

    /**
     * 重置密码第二步
     * @param map
     * @return
     */
    @RequestMapping(value = "/reset_password02",method = RequestMethod.PUT)
    @ResponseBody
    public ResponseMessage resetPassword02(@RequestBody Map<String, String> map){
        String email = "";
        String password  = "";
        String code = "";
        if (map.containsKey("email")){
            email = map.get("email");
            if (email == null){
                email = "";
            }
        }
        if (map.containsKey("password")){
            password = map.get("password");
            if (password == null){
                password = "";
            }
        }
        if (map.containsKey("code")){
            code = map.get("code");
            if (code == null){
                code = "";
            }
        }
//        这里也应该加一个验证码，实现短信邮箱发送验证码
        return userService.updatePassWord02(email, password, code);
    }

    /**
     * 获取用户信息
     * @param id
     * @param userId
     * @return
     */
    @RequestMapping(value = "/v1/user_info", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage getUserInfo(@RequestParam(name = "id", defaultValue = "-1") Integer id,
                                       @RequestParam(name = "userId", defaultValue = "-1") Integer userId){
        System.out.println(id);
        System.out.println(userId);
        return userService.getUserInfo(id,userId);
    }

    /**
     * 更改用户信息
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/v1/user_info", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseMessage getUserInfo(@RequestBody UserInfo userInfo){
       return userService.updateUserInfo(userInfo);
    }

    /**
     * 向邮箱发送注册验证码的接口
     * @param email
     * @return
     */
    @RequestMapping(value = "/register_code", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage sendRegisterMail(@RequestParam(name = "email") String email){
        return userService.sendRegisterMail(email);
    }

    /**
     * 向邮箱发送注册验证码的接口
     * @param email
     * @return
     */
    @RequestMapping(value = "/reset_pw_code", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage sendResetPwMail(@RequestParam(name = "email") String email){
        return userService.sendResetPwMail(email);
    }

    /**
     *
     * @param email
     * @return
     */
    @RequestMapping(value = "/email", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage isRegister(@RequestParam(name = "email") String email){
        return userService.isRegister(email);
    }
}
